package com.magic.gre.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.gre.R;
import com.magic.gre.base.activity.BaseMVPActivity;
import com.magic.gre.helper.Apphelper;
import com.magic.gre.mvp.contract.SetModifyPwsdContract;
import com.magic.gre.mvp.presenter.SetModifyPwsdPrersenterImpl;
import com.noname.lib_base_java.util.ToastUtil;
import com.noname.lib_base_java.util.ValidateUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: Lmoumou
 * Date: 2019-01-11 10:02
 * 文件名: SetModifyPwsdActivity
 * 描述:
 */
public class SetModifyPwsdActivity extends BaseMVPActivity<SetModifyPwsdPrersenterImpl> implements SetModifyPwsdContract.View {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.phone_edt)
    TextView phoneEdt;
    @BindView(R.id.code_edt)
    EditText codeEdt;
    @BindView(R.id.code_tv)
    TextView codeTv;
    @BindView(R.id.pwsd_edt)
    EditText pwsdEdt;
    @BindView(R.id.pwsd_edt2)
    EditText pwsdEdt2;
    @BindView(R.id.sumbit_bt)
    Button sumbitBt;

    public static void startThis(Context context) {
        Intent intent = new Intent(context.getApplicationContext(), SetModifyPwsdActivity.class);
        context.startActivity(intent);
    }

    private String code;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_modify_pwsd;
    }

    @Override
    protected SetModifyPwsdPrersenterImpl creatPresenter() {
        return new SetModifyPwsdPrersenterImpl(this);
    }

    @Override
    protected void netOverride() {

    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        phoneEdt.setText(Apphelper.getUserInfo().getPhone());
    }

    @OnClick(value = {R.id.code_tv, R.id.sumbit_bt})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.code_tv:
                String phone = String.valueOf(phoneEdt.getText());
                if (!ValidateUtil.Mobile(phone)) {
                    ToastUtil.getInstance().showNormal(this, "请输入正确手机号");
                    return;
                }
                showLoadDialog();
                codeTv.setEnabled(false);
                mPresenter.pSmsCode(phone, 3);
                break;
            case R.id.sumbit_bt:
                String phone2 = String.valueOf(phoneEdt.getText());
                if (!ValidateUtil.Mobile(phone2)) {
                    ToastUtil.getInstance().showNormal(this, "请输入正确手机号");
                    return;
                }

                String smsCode = String.valueOf(codeEdt.getText());
                if (!smsCode.equals(code)) {
                    ToastUtil.getInstance().showNormal(this, "请输入正确验证码");
                    return;
                }

                String pwsd1 = String.valueOf(pwsdEdt.getText());
                String pwsd2 = String.valueOf(pwsdEdt2.getText());

                if (pwsd1.length()<6||pwsd1.length()>20||pwsd2.length()<6||pwsd2.length()>20){
                    ToastUtil.getInstance().showNormal(this, "密码不得低于6位并且不得高于20位");
                    return;
                }

                if (!pwsd1.equals(pwsd2)){
                    ToastUtil.getInstance().showNormal(this, "密码输入不一致");
                    return;
                }

                mPresenter.pModify(phone2,pwsd1);

                break;
        }
    }

    @Override
    public void vSmsCode(String code) {
        this.code = code;

        hideLoadDialog();
        ToastUtil.getInstance().showNormal(this, code);
        cutdown(60 * 1000);
    }

    @Override
    public void vModify() {
        LoginActivity.startThis(this);
        finish();
    }

    private Handler handler = new Handler();

    @SuppressLint("SetTextI18n")
    private void cutdown(final int delay) {
        if (delay <= 0) {
            codeTv.setText("获取验证码");
            codeTv.setEnabled(true);
        } else {
            if (delay % 1000 == 0) {
                codeTv.setText(String.valueOf(delay / 1000) + "s");
            }
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    cutdown(delay - 100);
                }
            }, 100);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
